//
// Created by sgyt on 2020/3/7.
//  使用sigsetjmp,siglongjmp来对信号进行信号复位
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <setjmp.h>
#include <time.h>

static void sig_usrl(int);
static void sig_alrm(int);
static sigjmp_buf  sigjmpBuf;
static volatile sig_atomic_t canjmp;

int main(int argc,char *argv[])
{
    //注册信号
    if(signal(SIGUSR1,sig_usrl) < 0 )
    {
        perror("signal is error\n");
    };
    if(signal(SIGALRM,sig_alrm) < 0)
    {
        perror("signal is errpr\n");
    };

    printf("event start \n");
    //注册
    if(sigsetjmp(sigjmpBuf,1))
    {
        printf("event end\n");
        exit(0);
    }

    canjmp = 1;
    for(;;){
        pause();
    }
    return 0;
}


static void sig_usrl(int signo)
{
    time_t starttime;
    if(canjmp == 0)
    {
        return;
    }

    //开始执行定时发送SIGALRM信号
    printf("starting sig_usrl:[%d]\n",signo);
    alarm(3);
    starttime = time(NULL);
    for(;;)
    {
        if(time(NULL) > starttime+5)
        {
            break;
        }
    }
    printf("finish sig_usrl:[%d]\n",signo);

    canjmp = 0 ;
    siglongjmp(sigjmpBuf,1);
}

static void sig_alrm(int signo)
{
    printf("sign SIGALRM :[%d] is eventing\n",signo);
}
